//////////////////////////////////////
// Fast Container Library           //
// MIT License - Copyright (c) 2012 //
//////////////////////////////////////

#pragma once

#include "allocator.h"

FC_NAMESPACE_BEGIN


class linear_allocator
{
public:
	linear_allocator()
		: _poolBegin(0), _poolEnd(0), _poolCurrent(0)
		{
		}

	linear_allocator( void* ptrMemory, size_t nBytes )
		: _poolBegin(0), _poolEnd(0), _poolCurrent(0)
		{
			init( ptrMemory, nBytes );
		}

	void init( void* ptrMemory, size_t nBytes )
	{
		_poolBegin = _poolCurrent = (char*)ptrMemory;
		_poolEnd = ((char*)_poolBegin + nBytes);
	}

	void* allocate( size_t n, size_t alignment = 0 )
	{
		void* p = fc::align( _poolCurrent, alignment );
		if( ((char*)p + n) > _poolEnd )
			return 0;

		_poolCurrent += size_t((char*)p - _poolCurrent) + n;
		return p;
	}

	void deallocate( void*, size_t )
	{
		//do nothing
	}

	bool can_allocate() const
	{
		return (_poolCurrent >= _poolBegin && _poolCurrent < _poolEnd);
	}

	bool operator ==( const pool_allocator& ) { return false; }
	bool operator !=( const pool_allocator& ) { return true; }

protected:
	char* _poolBegin;
	char* _poolEnd;
	char* _poolCurrent;

};



FC_NAMESPACE_END

